/**
 * Copyright @ 2000 ysten Co. Ltd.
 * All right reserved.
 * @author: wangmin
 * date: 2016-05-13
 */

$(function () {
    $('#caseGrid').datagrid({
        method: 'get',
        singleSelect: true,
        checkOnSelect: false,
        selectOnCheck: false,
        fitColumns: true,
        rownumbers: true,
        pagination: true,
        pageSize: 20,
        shadow: false,
        data:[],
        columns: [
            [
                {field: 'ck', checkbox: true},
                {title: '<b>案例名称</b>', field: 'name', align: 'center', width: 100, sort: 'clientSide'},
                {title: '<b>PDF文件</b>', field: 'pdfUrl', align: 'center', width: 150, sort: 'clientSide',
                    formatter: function (value, row, index) {
                        if (row.pdfUrl) {
                            return '<a target="_blank" class="media" href="' + row.pdfUrl + '">查看PDF' + '</a>';
                        } else {
                            return "";
                        }
                    }},
                {title: '<b>创建日期</b>', field: 'createDate', align: 'center', width: 100, sort: 'clientSide'}
            ]
        ]
    });
    $('#customerGrid').datagrid({
        url: 'findCustomerPage.json',
        singleSelect: true,
        fitColumns: true,
        rownumbers: true,
        pagination: true,
        pageSize: 20,
        shadow: false,
        columns: [
            [
                {title: '<b>客户主键</b>', field: 'id', align: 'center', width: 50, sort: 'clientSide'},
                {title: '<b>客户名称</b>', field: 'name', align: 'center', width: 150, sort: 'clientSide'},
                {title: '<b>客户备注</b>', field: 'description', align: 'center', width: 250, sort: 'clientSide'}
            ]
        ],
        onClickRow: function (index, row) {
            $('#customerId').val(row.id);
            $('#caseGrid').datagrid({url: 'findCasePageByCustomerId.json?customerId=' + row.id});
        },
        onLoadSuccess: function (data) {
            if (data.rows.length > 0) {
                $('#customerId').val(data.rows[0].id);
                $('#caseGrid').datagrid({url: 'findCasePageByCustomerId.json?customerId=' + data.rows[0].id});
            } else {
                $('#customerId').val(null);
            }
        }
    });
    $('a.media').media();
    $("#queryCustomerBtn").click(function () {
        $('#customerGrid').datagrid('load', {
            search_LIKE_name: $("#searchCustomerName").textbox("getValue")
        });
    });
    $("#bindCustomer").click(function () {
        $('#queryFormBind').form('clear');
        $('#addCaseDialog').dialog({
            title: '待关联的案例',
            modal: true
        });
        $('#addCaseDialog').dialog('open').dialog('center');
        $('#to_bind_case_grid').datagrid({
            url: 'findNoBindCasePageByCustomerId.json?customerId=' + $("#customerId").val(),
            singleSelect: true,
            checkOnSelect: false,
            selectOnCheck: false,
            fitColumns: true,
            pagination: true,
            pageSize: 20,
            striped: true,
            rownumbers: true,
            columns: [
                [
                    {field: 'ck', checkbox: true},
                    {title: '<b>ID</b>', field: 'id', align: 'center', sort: 'clientSide', width: 50},
                    {title: '<b>案例名称</b>', field: 'name', align: 'center', width: 100, sort: 'clientSide'},
                    {title: '<b>PDF文件</b>', field: 'pdfUrl', align: 'center', width: 150, sort: 'clientSide',
                        formatter: function (value, row, index) {
                            if (row.pdfUrl) {
                                return '<a class="media" href="' + row.pdfUrl + '">查看PDF' + '</a>';
                            } else {
                                return "";
                            }
                        }},
                    {title: '<b>创建日期</b>', field: 'createDate', align: 'center', width: 100, sort: 'clientSide'}
                ]
            ]
        });
    });
    $("#unBindCustomer").click(function () {
        var selections = $('#caseGrid').datagrid('getChecked');
        if (selections.length == 0) {
            EGIFT_APP.showTip("请选择案例");
            return false;
        }
        $.messager.confirm('确认解除关联', '你确定要执行该操作吗？', function (r) {
            if (r) {
                var customerId = $("#customerId").val();
                var caseIds = EGIFT_APP.convertIds2Str(selections);
                if (customerId && caseIds) {
                    $.post("removeCustomerCase.json", {customerId: customerId, caseIds: caseIds}, function (obj) {
                        if (obj.result) {
                            $.messager.show({
                                title: '成功',
                                msg: obj.msg
                            });
                        } else {
                            $.messager.show({
                                title: '失败',
                                msg: obj.msg
                            });
                        }
                        $('#caseGrid').datagrid('reload');
                    })
                } else {
                    EGIFT_APP.showTip("请选择专题或专题内容");
                }
            }
        });
    });
    $("#queryCaseBtn").click(function () {
        $('#caseGrid').datagrid('load', {
            caseName: $("#queryCaseName").textbox("getValue")
        });
    });
    $("#queryToBindCaseBtn").click(function () {
        $('#to_bind_case_grid').datagrid('load', {
            caseName: $("#searchCaseName").textbox("getValue")
        });
    });
    $("#submitBindedCase").click(function () {
        var selections = $('#to_bind_case_grid').datagrid('getChecked');
        if (selections.length == 0) {
            EGIFT_APP.showTip("请选择案例");
            return false;
        }
        var customerId = $("#customerId").val();
        var caseIds = EGIFT_APP.convertIds2Str(selections);
        if (customerId && caseIds) {
            $.post("saveCustomerCase.json", {customerId: customerId, caseIds: caseIds}, function (obj) {
                if (obj.result) {
                    $.messager.show({
                        title: '成功',
                        msg: obj.msg
                    });
                } else {
                    $.messager.show({
                        title: '失败',
                        msg: obj.msg
                    });
                }
                $('#addCaseDialog').dialog('close');
                $('#caseGrid').datagrid('reload');
            })
        } else {
            EGIFT_APP.showTip("请选择客户");
        }
    });
})
